package handler

import (
	"bbs-web/internal/domain"
	"bbs-web/internal/repository/dao"
	"bbs-web/internal/service"
	"bbs-web/internal/web/jwtx"
	"bbs-web/internal/web/resp"
	"bbs-web/internal/web/vo"
	"bbs-web/pkg/ginplus"
	"bbs-web/pkg/tools"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
)

type UserHandler struct {
	svc    service.IuserService
	capSvc service.IcaptchaService
	jwtx.JwtHandler
}

func NewuserHandler(svc service.IuserService, capSvc service.IcaptchaService, j jwtx.JwtHandler) *UserHandler {
	return &UserHandler{
		svc:        svc,
		capSvc:     capSvc,
		JwtHandler: j,
	}
}

func (u *UserHandler) Register(ctx *gin.Context, req vo.RegisterUserReq) (ginplus.Result, error) {
	//两次密码是否正确
	if req.Password != req.RPassword {
		return ginplus.Result{Msg: "两次输入密码不一致"}, errors.New("两次输入密码不一致")
	}

	//校验验证码是否正确
	ok := u.capSvc.CheckCaptcha(req.CaptchaId, req.CaptchaCode, true)
	if !ok {
		return ginplus.Result{Msg: "验证码错误"}, errors.New("验证码错误")
	}

	//校验密码强度
	ok, err := tools.CheckPasswordStrength(req.Password)
	if err != nil {
		return ginplus.Result{
			Msg: err.Error(),
		}, err
	}
	if !ok {
		return ginplus.Result{
			Msg: err.Error(),
		}, err
	}

	err = u.svc.SignUp(ctx, domain.UserInfo{
		ID:       0,
		UserName: req.Username,
		Password: req.Password,
	})
	//写入数据库发现是重复用户的报错
	if err == dao.ErrUserDuplicate {
		return ginplus.Result{
			Code: 501000,
			Msg:  err.Error(),
		}, err
	}
	if err != nil {
		return ginplus.Result{
			Msg: "系统异常",
		}, err
	}
	return ginplus.Result{
		Msg: "注册成功",
	}, nil
}

func (u *UserHandler) PwdLogin(ctx *gin.Context, req vo.PwdLoginReq) (ginplus.Result, error) {
	//校验验证码
	if !u.capSvc.CheckCaptcha(req.CaptchaId, req.CaptchaCode, false) {
		return ginplus.Result{Msg: "验证码错误"}, nil
	}

	//校验密码和用户
	data, err := u.svc.Login(ctx, domain.UserInfo{
		ID:       0,
		UserName: req.UserName,
		Password: req.Password,
	})
	if err != nil {
		return ginplus.Result{Msg: err.Error()}, err
	}

	//获取token
	token, er := u.JwtHandler.GetJWTToken(ctx, int64(data.ID))
	if er != nil {
		return ginplus.Result{Msg: err.Error()}, nil
	}

	// 组装response数据
	response := resp.PWDLoginResp{
		Token:    "Bearer " + token,
		UserName: data.Username,
		UserId:   int64(data.ID),
		Cover:    data.Avatar,
	}
	fmt.Println(response)

	return ginplus.Result{
		Data: response,
	}, nil
}

func (u *UserHandler) CreateUser(ctx *gin.Context, req vo.CreateUserReq, userToken jwtx.UserClaims) (ginplus.Result, error) {

	data, err := u.svc.CreateUser(ctx, domain.UserInfo{
		ID:       userToken.Id,
		UserName: req.UserName,
		Phone:    req.Phone,
		Password: req.Password,
		IsAdmin:  req.IsAdmin,
	})

	// 组装response数据
	response := resp.RegUserResponse{
		Id:       data.ID,
		UserName: data.Username,
	}

	if err != nil {
		return ginplus.Result{Msg: "创建用户失败"}, err
	}
	return ginplus.Result{
		Msg:  "创建用户成功",
		Data: response,
	}, nil
}

//func (u *UserHandler) GetUserList(ctx *gin.Context, req vo.UserSearchReq, userToken jwtx.UserClaims) (ginplus.Result, error) {
//	u.svc.SearchUserList(ctx,
//}
